"""
PySCeS_CBM: Constraint Based Modelling with PySCeS.
Brett G. Olivier (bgoli@users.sourceforge.net)

(c) Brett G. Olivier, Vrije Universiteit Amsterdam, Amsterdam 2009-2011.
All rights reserved.
"""

"""
E. coli core model
"""

import pyscescbm
from pyscescbm.CBVersion import __DEBUG__, __version__
import os, time
cDir = os.path.dirname(os.path.abspath(os.sys.argv[0]))
import numpy, sympy
from pyscescbm import CBModel, CBRead, CBWrite, CBTools, CBModelTools, CBXML
import pyscescbm.CBSolver as slv

work_dir = os.path.join(cDir, 'init_model')
work_dir2 = os.path.join(cDir, 'steven')
if not os.path.exists(work_dir):
    os.mkdir(work_dir)
    os.mkdir(work_dir2)

from CoreModelDefinitions import Define_core_memesa_model
model_name, Reactions, Species, Bounds, Objective_function = Define_core_memesa_model()

CBModel.__DEBUG__ = True
##  fba = CBModel.Model(model_name)
##  CBModelTools.addSpecies(fba, Species)
##  CBModelTools.addReactions(fba, Reactions)
##  CBModelTools.addObjectiveFunction(fba, Objective_function)
##  CBTools.addStoichToFBAModel(fba)
##  CBTools.exportLabelledArray(fba.N(), fba.N.row, os.path.join(work_dir, model_name+'_N.txt'), sep=' ', format='%2i')

##  Bounds = CBModelTools.addReversibilityBounds(Reactions, Bounds, 1000)
##  CBModelTools.addBounds(fba, Bounds)

fba = CBModelTools.quickDefaultBuild(model_name, Reactions, Species, Bounds, Objective_function, infinity=1000)
fba.setPid('core_memesa_model')
CBTools.addStoichToFBAModel(fba)
CBTools.exportLabelledArray(fba.N(), fba.N.row, os.path.join(work_dir, model_name+'_N.txt'), sep=' ', format='%2i')

##  slv.analyzeModel(fba, lpFname=os.path.join(work_dir, model_name+'.1'))

print fba.N.row
print fba.N.col
print fba.N()
print fba.N.shape

CBModelTools.printSolution(fba, False)

#fva_result, fva_names = slv.cplx_FluxVariabilityAnalysis(fba, pre_opt=True, tol=None, rhs_sense='lower', optPercentage=100.0, work_dir=work_dir, debug=False)
#slv.cplx_WriteFVAtoCSV(fba.getPid()+'.1', fva_result, fva_names, Dir=work_dir, fbaObj=fba)

##  dupl_react = CBTools.scanForReactionDuplicates(fba)

##  if len(dupl_react) > 0:
    ##  print '\nDuplicate reactions detected', dupl_react

slv.analyzeModel(fba)

CBTools.exportLabelledArray(fba.N(), fba.N.row, os.path.join(work_dir, model_name+'_N.txt'), sep=' ', format='%2i')
CBWrite.exportModel(fba, fmt='lp', work_dir=work_dir)
CBWrite.writeSBML2FBA(fba, os.path.join(work_dir,'core_memesa_model.xml'))

fba = CBXML.readSBML2FBA('core_memesa_model.xml', work_dir=work_dir)

slv.analyzeModel(fba)


##  fba.id = fba.getPid()+'.noinf.cplex'
print '\nAttempting to delete bounds for biomass reaction,', fba.getActiveObjective().getFluxObjectiveReactions()[0]
fba.deleteBoundsForReactionId(fba.getActiveObjective().getFluxObjectiveReactions()[0])

tmp_mid = fba.id
fba.id = fba.id+'_withinf'
CBWrite.exportModel(fba, fmt='hformat', work_dir=work_dir2)
fba.id = tmp_mid
print '\n%s\n' % len(fba.flux_bounds)
fba.changeAllFluxBoundsWithValue(1000, 'Infinity')
fba.changeAllFluxBoundsWithValue(-1000, '-Infinity')
print '\n%s\n' % len(fba.flux_bounds)
slv.analyzeModel(fba)
##  exportModel(fba, fmt='lp', work_dir=work_dir)
CBWrite.exportModel(fba, fname=fba.getPid()+'.noinf.cplex', fmt='lp', work_dir=work_dir2)

fba.id = fba.id.replace('.cplex','')
print '\n%s\n' % len(fba.flux_bounds)
fba.deleteAllFluxBoundsWithValue('Infinity')
fba.deleteAllFluxBoundsWithValue('-Infinity')
print '\n%s\n' % len(fba.flux_bounds)
##  CBWrite.exportModel(fba, fmt='all', work_dir=work_dir)
CBWrite.exportModel(fba, fname=fba.getPid().replace('_cplex','')+'.noinf', fmt='all', work_dir=work_dir2)
